class EventBus {
  constructor(){
    //创建一个数据源
    this.cache = {}
  }
  on(name, fn){
    if(!this.cache[name]) {
      this.cache[name] = []
    }
    this.cache[name].push(fn)
  }
  off(name, fn){
    if(!name) {
      this.cache = {}
      return
    }
    if(!fn) {
      delete this.cache[name]
      return
    }
    const listeners = this.cache[name];
    if(listeners) {
      this.cache[name] = listeners.filter(f => f!==fn)
    }
  }
  emit(name, once=false, ...args){
    if(this.cache[name]) {
      this.cache[name].forEach(fn => {
        fn(...args)
      })
      once && this.off(name)
    }
  }
}